package cn.zqh.auth.service.impl;

import cn.zqh.auth.config.BSException;
import cn.zqh.bean.api.CommonResult;
import cn.zqh.bean.api.ResultCode;
import cn.zqh.bean.bo.AddANewRouter;
import cn.zqh.bean.bo.AdminChangeRouter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zqh.auth.mapper.RouterMapper;
import cn.zqh.auth.pojo.Router;
import cn.zqh.auth.service.RouterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-12-22
 */
@Service
public class RouterServiceImpl extends ServiceImpl<RouterMapper, Router> implements RouterService {

    @Resource
    private RouterMapper routerMapper;


    /**
      * @Author: zqh
      * @Date:
      * @Description: 添加一个新路由
    */
    @Override
    public CommonResult addANewRouterInfo(AddANewRouter addANewRouter) {
        Router router = new Router();
        router.setRouterName(addANewRouter.getRouterName())
                .setRouterDesc(addANewRouter.getRouterDesc())
                .setRouterPermission(addANewRouter.getRouterPermission())
                .setIsDelete(0);
        int i = routerMapper.insert(router);
        if (i != 1){
            throw new BSException(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
        }
        return CommonResult.success(ResultCode.SUCCESS);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改某个路由的信息
    */
    @Override
    public CommonResult modifyARouteInformation(AdminChangeRouter adminRouter) {
        Router router = routerMapper.selectOne(new QueryWrapper<Router>().eq("Router_id", adminRouter.getRouterId()));
        if (router == null){
            throw new BSException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMessage());
        }
        router.setRouterName(adminRouter.getRouterName())
                .setIsDelete(adminRouter.getIsDel())
                .setRouterDesc(adminRouter.getRouterDesc())
                .setRouterPermission(adminRouter.getRouterPermission());

        int i = routerMapper.updateById(router);
        if (i != 1){
            throw new BSException(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
        }
        return CommonResult.success(ResultCode.SUCCESS);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过id查询路由
    */
    @Override
    public CommonResult getRouterById(Integer routeId) {
        Router router = routerMapper.selectById(routeId);
        if (router == null){
            throw new BSException(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
        }
        return CommonResult.success(router);
    }
    /**
      * @Author: zqh
      * @Date:
      * @Description: 查询所有路由
    */
    @Override
    public CommonResult getAllRouter() {
        List<Router> routers = routerMapper.selectList(null);
        return CommonResult.success(routers);
    }
}
